package com.mallcai.bigdata.ladon.dpl.mapper.system;

import com.mallcai.bigdata.ladon.dpl.entity.system.Resource;
import com.mallcai.bigdata.ladon.dpl.mapper.system.provider.ResourceSqlProvider;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author gst
 * @date 2019-12-30 20:36
 */
@Repository
public interface ResourceMapper {

    @InsertProvider(type = ResourceSqlProvider.class, method = "insert")
    public int insert(Resource res);

    @UpdateProvider(type = ResourceSqlProvider.class, method = "update")
    public int update(Resource res);

    @UpdateProvider(type = ResourceSqlProvider.class, method = "delete")
    public int delete(Resource res);

    @SelectProvider(type = ResourceSqlProvider.class, method = "getByParams")
    public List<Resource> getByParams(@Param("params") Map<String, Object> params);

    @SelectProvider(type = ResourceSqlProvider.class, method = "getChildren")
    public List<Resource> getChildren(@Param("parentIds") List<Long> parentIds);

    @SelectProvider(type = ResourceSqlProvider.class, method = "findById")
    public Resource getById(@Param("id") Long id);

    @SelectProvider(type = ResourceSqlProvider.class, method = "getByIds")
    public List<Resource> getByIds(@Param("ids") List<Long> ids);

    @Select({
            "SELECT id, icon, name, path, level" +
                    " FROM " + ResourceSqlProvider.TB_RES +
                    " WHERE archive = 0 AND level <= 1 " +
                    " ORDER BY sort_id ASC "
    })
    public List<Resource> getParentRes();

    @Select({
            "SELECT * FROM " + ResourceSqlProvider.TB_RES +
                    " WHERE archive = 0 " +
                    " ORDER BY parent_id, sort_id "
    })
    public List<Resource> getAllRes();

    @Select({
            "SELECT count(0) FROM " + ResourceSqlProvider.TB_RES +
                    " WHERE archive = 0 AND parent_id = #{id}"
    })
    public int hasChildren(@Param("id") Long id);

    @Select({
            "SELECT * FROM " + ResourceSqlProvider.TB_RES
                    + " WHERE archive = 0 AND (path = #{path} OR name =#{name})"
    })
    public List<Resource> hasSame(@Param("name") String name, @Param("path") String path);

    @SelectProvider(type = ResourceSqlProvider.class, method = "getAuthCodeByUserId")
    public List<Resource> getAuthCodeByUserId(@Param("userId") Long userId);

    @Select({
            "SELECT id, parent_id, auth_code FROM " + ResourceSqlProvider.TB_RES + " WHERE archive = 0 ORDER BY parent_id, sort_id"
    })
    public List<Resource> getAllSimpleRes();
}
